package com.ebank.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class EditPhoneNumberCL extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		PrintWriter out = response.getWriter();
		if(session.getAttribute("username")!=null){
		Statement stat = null;
		Connection con=null;
		ResultSet rs=null;
		PreparedStatement pst = null;
		String selectSQL = "select * from information";
		String loginName=(String) (session.getAttribute("username"));
		int phonenumber=Integer.parseInt(request.getParameter("phonenumber"));
try {
	Class.forName("com.mysql.jdbc.Driver");
	con = DriverManager.getConnection(
	      "jdbc:mysql://localhost/user?useUnicode=true&characterEncoding=Big5",
	      "root","root");
	 pst=con.prepareStatement("UPDATE information SET phone=? where username=?");
	 pst.setObject(1, phonenumber);
	 pst.setObject(2, loginName);
	 pst.execute();
	 out.print("Success.......Please Wait");
	 response.setHeader("refresh", "2;URL=Main");
} catch (Exception e) {
	}finally{
		if(rs!=null){
			try {
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			rs=null;
		}
		if(pst!=null){
			try {
				pst.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			pst=null;
		}
		if(con!=null){
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			con=null;
		}
	}}else{ out.print("Wong Please Login <a href=/EBanking/Login>Login</a>");
	} 
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
	}

}
